﻿$j(function () {
    var mainMenuList;
    xmlDoc = loadXMLDoc('../../XMLConfig/TreeMenu.xml');

    createTable(xmlDoc);

    function loadXMLDoc(url) {
        var xmlDoc;
        try {
            if (window.ActiveXObject) {
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async = false;
                xmlDoc.load(url);
                isIE = true;
                return xmlDoc;
            }
            else if (document.implementation && document.implementation.createDocument) {
                xmlDoc = document.implementation.createDocument("", "", null);
                xmlDoc.async = false;
                xmlDoc.load(url);
                return xmlDoc;
            }
        }
        catch (e) {
            try {
                var oXmlHttp = new XMLHttpRequest();
                oXmlHttp.open("GET", url, false);
                oXmlHttp.send(null);
                var str = oXmlHttp.responseText;
                return (new DOMParser()).parseFromString(str, "text/xml");
            } catch (e) {
                return;
            }
        }
        xmlDoc.async = false;
        xmlDoc.load(url);
        return xmlDoc;
    };

    $j("#main_menu td span").click(function () {
        var index = $j(this).attr("index");
        var strSubMenu = showTreeSubMenu(mainMenuList[parseInt(index, 10)], "");
        $j("#sub_menu").html("");
        $j("#sub_menu").append(strSubMenu);
    });
    function createTable(xmlDoc) {
        var strMainMenu = "";
        //main_menu取得
        mainMenuList = xmlDoc.getElementsByTagName("menu_0");
        for (var i = 0; i < mainMenuList.length; i++) {
            strMainMenu += "<td><span style='cursor: pointer;'  index=\"" + i + "\">" + mainMenuList[i].getAttribute("title") + "</span></td>";
        }
        $j("#main_menu").append(strMainMenu);
        var strSubMenu = showTreeSubMenu(mainMenuList[0], "");
        $j("#sub_menu").append(strSubMenu);
    };
    function showTreeSubMenu(parent_menu, strSubMenu) {
        //sub_menu取得
        var deepth = parseInt(parent_menu.getAttribute("deep"), 10);

        var sub_menu = parent_menu.getElementsByTagName("menu_" + (deepth + 1));

        if (sub_menu.length > 0) {
            for (var i = 0; i < sub_menu.length; i++) {
                strSubMenu += "<div style='height:40px;width: 70px;'><span style='cursor: pointer;' index=\"" + i + "\">" + sub_menu[i].getAttribute("title") + "</span></div>";
                strSubMenu += showTreeSubMenu(sub_menu[i], "");
            }
            return strSubMenu;
        }
        else {
            return "";
        }
    };
    //退出事件绑定
    $j("#close").click(function () {
        window.location = "/Admin/Login";
    });
    //获取当前日期
    function current() {
        var d = new Date(), str = '今天是';
        str += d.getFullYear() + '年'; //获取当前年份 
        str += d.getMonth() + 1 + '月'; //获取当前月份（0——11） 
        str += d.getDate() + '日';
        str += ((d.getFullYear() % 4 == 0 && d.getFullYear() % 100 != 0) || (d.getFullYear() % 400 == 0)) ? "<span style='font-size:11px;'>(闰年)</span>" : "<span style='font-size:11px;'>(平年)</span>";
        str += " " + d.toLocaleTimeString() + " ,星期";
        str += "天一二三四五六".charAt(d.getDay());
        return str;
    }
    setInterval(function () { $j("#nowTime").html(current) }, 1000);
});